﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Threading;
using System.Windows.Threading;
using Controller.ShakeGestures;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

namespace OmnipotentFlashlight
{
    public partial class screen : PhoneApplicationPage
    {
        public screen()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(screen_Loaded);
        }
        DispatcherTimer timer = null;
        SoundEffect effect = null;
        int c = 0;
        int d = 0;
        int f = 0;
        int c2 = 0;
        int d2 = 0;
        int f2 = 0;
        bool isLoing = false;
        void screen_Loaded(object sender, RoutedEventArgs e)
        {
            if (NavigationContext.QueryString["action"] == "sos")
            {
                timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromMilliseconds(100);
                timer.Tick += new EventHandler(timer_Tick);
                timer.Start();
            }
            else
            {
                Shake();
            }
        }

        void Shake()
        {
            ShakeGesturesHelper.Instance.ShakeGesture += (s, e) =>
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    effect.Play(); ChangeColor();
                });
            };
            string sha = Common.GetSetting("screen_shake");
            int b = (sha != null && sha != "0") ? (Convert.ToInt32(sha) * 2) : 5;
            ShakeGesturesHelper.Instance.MinimumRequiredMovesForShake = b;
            effect = SoundEffect.FromStream(TitleContainer.OpenStream("icon/Shake.wav"));
            ShakeGesturesHelper.Instance.Active = (sha != null && sha != "0");
            FrameworkDispatcher.Update();
        }


        void ChangeColor()
        {
            System.Windows.Media.Color color = ((SolidColorBrush)ContentPanel.Background).Color;
            if (color == Colors.White)
            {
                ContentPanel.Background = new SolidColorBrush(Colors.Yellow);
            }
            else if (color == Colors.Yellow)
            {
                ContentPanel.Background = new SolidColorBrush(Colors.Green);
            }
            else if (color == Colors.Green)
            {
                ContentPanel.Background = new SolidColorBrush(Common.HtmlToColor("#CCFFFF"));
            }
            else if (color == Common.HtmlToColor("#CCFFFF"))
            {
                ContentPanel.Background = new SolidColorBrush(Colors.Blue);
            }
            else if (color == Colors.Blue)
            {
                ContentPanel.Background = new SolidColorBrush(Colors.Gray);
            }
            else if (color == Colors.Gray)
            {
                ContentPanel.Background = new SolidColorBrush(Colors.Red);
            }
            else if (color == Colors.Red)
            {
                ContentPanel.Background = new SolidColorBrush(Colors.White);
            }
        }



        void timer_Tick(object sender, EventArgs e)
        {
            if (isLoing)
            {
                if (c2 != 0 && c2 % 15 == 0)
                {
                    SetColor(true);
                    d2 = c2;
                }
                else
                {
                    if (d2 == c2 - 7)
                    {
                        SetColor(false);
                        f2++;
                        if (f2 >= 3)
                        {
                            f = 0; f2 = 0;
                            isLoing = false;
                        }
                    }
                }
                c2++;
            }
            else
            {
                if (c % 10 == 0)
                {
                    SetColor(true);
                    d = c;
                }
                else
                {
                    if (d == c - 2)
                    {
                        SetColor(false);
                        f++;
                        if (f >= 3) isLoing = true;
                    }
                }
                c++;
            }
        }


        void SetColor(bool color)
        {
            if (color)
            {
                ContentPanel.Background = new SolidColorBrush(Colors.White);
            }
            else
            {
                ContentPanel.Background = new SolidColorBrush(Colors.Black);
            }
        }
    }
}
